﻿
Imports DotNetNuke.Services.Social.Notifications
Imports DotNetNuke.Entities.Users
Imports DotNetNuke.Security.Roles
Imports System.Linq

Namespace Components.Integration
    Public Class Notifications
        Public Const ContentTypeName As String = "RTPForgottenUser"
        Public Const NotificationUsernameSentTypeName As String = "FUSentUsername"

        ''' <summary>
        ''' Informs the core journal that the user has posted a blog entry.
        ''' </summary>
        ''' <param name="SendToRoles"></param>
        ''' <remarks></remarks>
        Friend Sub SendUsernameNotification(ByVal Subject As String, ByVal Body As String, ByVal SendToUsers As System.Collections.Generic.List(Of UserInfo), ByVal SenderUserid As Integer, ByVal PortalID As Integer)
            Dim notificationType As NotificationType = NotificationsController.Instance.GetNotificationType(NotificationUsernameSentTypeName)
            If notificationType Is Nothing Then
                Dim deskModuleId As Integer = DotNetNuke.Entities.Modules.DesktopModuleController.GetDesktopModuleByModuleName("ForgottenUser", PortalID).DesktopModuleID
                Dim objNotificationType As NotificationType = New NotificationType
                objNotificationType.Name = NotificationUsernameSentTypeName
                objNotificationType.Description = "Forgotten Username Sent"
                objNotificationType.DesktopModuleId = deskModuleId
                NotificationsController.Instance.CreateNotificationType(objNotificationType)
                notificationType = NotificationsController.Instance.GetNotificationType(NotificationUsernameSentTypeName)
            End If

            Dim notificationKey As String = String.Format("{0}:{1}:{2}", ContentTypeName + NotificationUsernameSentTypeName, SendToUsers.Item(0).UserID, Date.UtcNow.ToString("yyyyMMddHHmmss"))
            Dim objNotification As New Notification
            Dim objUserController As New UserController()
            Dim SenderUser As UserInfo = objUserController.GetUser(PortalID, SenderUserid)

            objNotification.NotificationTypeID = notificationType.NotificationTypeId
            objNotification.Subject = Subject
            objNotification.Body = Body
            objNotification.IncludeDismissAction = True
            objNotification.SenderUserID = SenderUserid
            objNotification.From = SenderUser.DisplayName
            objNotification.Context = notificationKey

            NotificationsController.Instance.SendNotification(objNotification, PortalID, Nothing, SendToUsers)
        End Sub

    End Class
End Namespace
